package com.sokal.easycare.modules.product.web.factory;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.product.entity.factory.ProFactory;
import com.sokal.easycare.modules.product.service.factory.ProFactoryService;

/**
 * 生产厂家Controller
 * @author liliangdong
 * @version 2017-03-12
 */
@Controller
@RequestMapping(value = "${adminPath}/product/factory/proFactory")
public class ProFactoryController extends BaseController {

	@Autowired
	private ProFactoryService proFactoryService;
	
	@ModelAttribute
	public ProFactory get(@RequestParam(required=false) String id) {
		ProFactory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = proFactoryService.get(id);
		}
		if (entity == null){
			entity = new ProFactory();
		}
		return entity;
	}
	
	@RequiresPermissions("product:factory:proFactory:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProFactory proFactory, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProFactory> list = proFactoryService.findList(proFactory); 
		model.addAttribute("list", list);
		return "modules/product/factory/proFactoryList";
	}

	@RequiresPermissions("product:factory:proFactory:view")
	@RequestMapping(value = "form")
	public String form(ProFactory proFactory, Model model) {
		if (proFactory.getParent()!=null && StringUtils.isNotBlank(proFactory.getParent().getId())){
			proFactory.setParent(proFactoryService.get(proFactory.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(proFactory.getId())){
				ProFactory proFactoryChild = new ProFactory();
				proFactoryChild.setParent(new ProFactory(proFactory.getParent().getId()));
				List<ProFactory> list = proFactoryService.findList(proFactory); 
				if (list.size() > 0){
					proFactory.setSort(list.get(list.size()-1).getSort());
					if (proFactory.getSort() != null){
						proFactory.setSort(proFactory.getSort() + 30);
					}
				}
			}
		}
		if (proFactory.getSort() == null){
			proFactory.setSort(30);
		}
		model.addAttribute("proFactory", proFactory);
		return "modules/product/factory/proFactoryForm";
	}

	@RequiresPermissions("product:factory:proFactory:edit")
	@RequestMapping(value = "save")
	public String save(ProFactory proFactory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proFactory)){
			return form(proFactory, model);
		}
		proFactoryService.save(proFactory);
		addMessage(redirectAttributes, "保存生产厂家成功");
		return "redirect:"+Global.getAdminPath()+"/product/factory/proFactory/?repage";
	}
	
	@RequiresPermissions("product:factory:proFactory:edit")
	@RequestMapping(value = "delete")
	public String delete(ProFactory proFactory, RedirectAttributes redirectAttributes) {
		proFactoryService.delete(proFactory);
		addMessage(redirectAttributes, "删除生产厂家成功");
		return "redirect:"+Global.getAdminPath()+"/product/factory/proFactory/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ProFactory> list = proFactoryService.findList(new ProFactory());
		for (int i=0; i<list.size(); i++){
			ProFactory e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}